#include "https.h"

QString server="http://127.0.0.1:8080";

Https::Https(QObject *parent)
    : QObject{parent}
{
    networkManager = new QNetworkAccessManager(this);
    connect(networkManager, &QNetworkAccessManager::finished, this, &Https::onFinished);
}

void Https::sendGetRequest(const QString &url)
{
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QSslConfiguration sslConfig = request.sslConfiguration();
    sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);// 禁用证书验证
    sslConfig.setProtocol(QSsl::TlsV1_2); // 设置使用 TLS 1.2 协议
    request.setSslConfiguration(sslConfig);

    networkManager->get(request);
}

void Https::sendPostRequest(const QString &url, const QJsonObject &jsonData)
{
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QSslConfiguration sslConfig = request.sslConfiguration();
    sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone); // 禁用证书验证
    sslConfig.setProtocol(QSsl::TlsV1_2); // 设置使用 TLS 1.2 协议
    request.setSslConfiguration(sslConfig);

    QJsonDocument jsonDoc(jsonData);
    QByteArray postData = jsonDoc.toJson();
    networkManager->post(request, postData);
}

void Https::sendPutRequest(const QString &url, const QJsonObject &jsonData)
{
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QSslConfiguration sslConfig = request.sslConfiguration();
    sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone); // 禁用证书验证
    sslConfig.setProtocol(QSsl::TlsV1_2); // 设置使用 TLS 1.2 协议
    request.setSslConfiguration(sslConfig);

    QJsonDocument jsonDoc(jsonData);
    QByteArray putData = jsonDoc.toJson();
    networkManager->put(request, putData);
}

void Https::sendDeleteRequest(const QString &url)
{
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QSslConfiguration sslConfig = request.sslConfiguration();
    sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone); // 禁用证书验证
    sslConfig.setProtocol(QSsl::TlsV1_2); // 设置使用 TLS 1.2 协议
    request.setSslConfiguration(sslConfig);

    networkManager->deleteResource(request);
}

void Https::login(const QString&username,const QString&password)
{
    QJsonObject obj;
    obj["user_name"]=username.toLongLong();
    obj["password"]=password.toStdString().c_str();
    sendPostRequest(server+"/login",obj);
}

void Https::onFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray responseData = reply->readAll();
        QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
        if (jsonDoc.isObject()) {
            QJsonObject jsonObj = jsonDoc.object();
            qDebug() << "JSON Response" ;
            parseJsonObject(jsonObj);
        } else {
            //qDebug() << "Invalid JSON response" ;
        }
    } else {
        qDebug() << "Error:" << reply->errorString();
    }
    reply->deleteLater();
}

void Https::parseJsonObject(const QJsonObject& obj)
{
    //qDebug()<<obj;
    if(obj["process"].toString()=="GetAlbumInfo")
    {
        //qDebug()<<"GetAlbumInfo";
        emit albumInfoReady(obj);
    }
    else if(obj["process"].toString()=="GetSongInfo")
    {
        //qDebug()<<"GetSongInfo";
        emit songInfoReady(obj);
    }
    else if(obj["process"].toString()=="Login")
    {
        //qDebug()<<"login";
        emit loginResponse(obj);
    }
    else if(obj["process"].toString()=="Search")
    {
        qDebug()<<obj;
        emit searchInfoReady(obj);
    }
    else if(obj["process"].toString()=="CreateAlbum")
    {
        qint64 id=obj["album_id"].toInteger();
        emit albumidReady(id);
    }

}
